package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.ComponentDTO;
import com.carkeeper.exception.BussinessException;
import com.carkeeper.mapper.ComponentMapper;
import com.carkeeper.pojo.ComponentBrandPO;
import com.carkeeper.pojo.ComponentPO;
import com.carkeeper.service.ComponentService;
import com.carkeeper.vo.ComponentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：XXX
 * @date ：Created in 2020/6/3
 * @description ：
 * @version: 1.0
 */
@Service
public class ComponentServiceImpl implements ComponentService {
    @Autowired
    private ComponentMapper componentMapper;

    /**
     * 根据ID查询配件
     *
     * @param id
     * @return
     */
    @Override
    public ComponentVO queryComponentById(Long id) {
        ComponentPO componentPO = componentMapper.findComponentById(id);
        if (componentPO == null) {
            throw new BussinessException("查找的对象不存在！");
        }
        ComponentVO componentVO = new ComponentVO();
        BeanUtils.copyProperties(componentPO, componentVO);
        return componentVO;
    }

    /**
     * 增加配件
     *
     * @param componentPO
     * @return
     */
    @Override
    public boolean add(ComponentPO componentPO) {
        return componentMapper.insert(componentPO) > 0;
    }

    /**
     * 修改配件信息
     *
     * @param componentPO
     * @return
     */
    @Override
    public boolean update(ComponentPO componentPO) {
        return componentMapper.updateById(componentPO) > 0;
    }

    /**
     * 根据ID删除配件
     *
     * @param componentId
     * @return
     */
    @Override
    public boolean delete(long componentId) {
        return componentMapper.deleteById(componentId) > 0;
    }

    @Override
    public Page<ComponentPO> query(Long page,
                                   Long pageSize,
                                   String storeName,
                                   String code,
                                   String componentName,
                                   String specification,
                                   String componenBrandName,
                                   String componentTypeName,
                                   Integer state) {
        Page<ComponentPO> pageResult = new Page<>(page, pageSize);
        LambdaQueryWrapper<ComponentPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(storeName)) {
            lambdaQueryWrapper.like(ComponentPO::getBusinessName, storeName);
        }
        if (!StringUtils.isEmpty(code)) {
            lambdaQueryWrapper.like(ComponentPO::getCode, code);
        }
        if (!StringUtils.isEmpty(componentName)) {
            lambdaQueryWrapper.like(ComponentPO::getComponentName, componentName);
        }
        if (!StringUtils.isEmpty(specification)) {
            lambdaQueryWrapper.like(ComponentPO::getSpecification, specification);
        }
        if (!StringUtils.isEmpty(componentTypeName)) {
            lambdaQueryWrapper.like(ComponentPO::getTypeName, componentTypeName);
        }
        if (state != null) {
            lambdaQueryWrapper.eq(ComponentPO::getState, state);
        }
        lambdaQueryWrapper.orderByDesc(ComponentPO::getCreateTime);
        Page<ComponentPO> poPage = componentMapper.selectPage(pageResult, lambdaQueryWrapper);
        return poPage;
    }

    @Override
    public List<ComponentDTO> getByIds(List<Long> ids) {
        List<ComponentDTO> dtoList = new ArrayList<>();
        List<ComponentPO> componentPOS = componentMapper.selectBatchIds(ids);
        for (ComponentPO componentPO : componentPOS) {
            ComponentDTO componentDTO = new ComponentDTO();
            BeanUtils.copyProperties(componentPO, componentDTO);
            dtoList.add(componentDTO);
        }
        return dtoList;
    }
}
